package com.llxwz.service;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.llxwz.dao.DiscussPostDao;
import com.llxwz.entity.DiscussPost;
import com.llxwz.entity.User;
import com.llxwz.util.SensitiveFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.util.HtmlUtils;

import java.util.List;

@Service
public class DiscussPostService {

    @Autowired
    DiscussPostDao discussPostDao;

    @Autowired
    SensitiveFilter sensitiveFilter;

    public List<DiscussPost> findDiscussPosts(int userId, int current, int limit){
        QueryWrapper wrapper = new QueryWrapper();
        if (userId!=0){
            wrapper.eq("user_id",userId);
        }
        wrapper.ne("status",2);
        wrapper.orderByDesc("type","create_time");
        Page<DiscussPost> result = discussPostDao.selectPage(new Page<>(current,limit), wrapper);

        return result.getRecords();
    }

    public int findDiscussPostRows(int userId){
        QueryWrapper wrapper = new QueryWrapper();
        if (userId!=0){
            wrapper.eq("user_id",userId);
        }
        wrapper.ne("status",2);
        return discussPostDao.selectCount(wrapper);
    }


    public int addDiscussPost(DiscussPost post){
        if(post == null){
            throw new IllegalArgumentException("参数不存在");
        }

        //转义HTML标记
        post.setTitle(HtmlUtils.htmlEscape(post.getTitle()));
        post.setContent(HtmlUtils.htmlEscape(post.getContent()));

        //过滤敏感词
        post.setTitle(sensitiveFilter.filter(post.getTitle()));
        post.setContent(sensitiveFilter.filter(post.getContent()));

        return discussPostDao.insert(post);

    }

    public DiscussPost findDiscussPostById(int id){
        return discussPostDao.selectById(id);
    }

    public int updateCommentCount(int id,int commentCount){
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id",id);
        updateWrapper.set("comment_count",commentCount);
        return discussPostDao.update(null,updateWrapper);
    }

    public int updateType(int id, int type) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id",id);
        updateWrapper.set("type",type);
        return discussPostDao.update(null,updateWrapper);
    }

    public int updateStatus(int id, int status) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id",id);
        updateWrapper.set("status",status);
        return discussPostDao.update(null,updateWrapper);
    }

    public int updateScore(int id, double score) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id",id);
        updateWrapper.set("score",score);
        return discussPostDao.update(null,updateWrapper);
    }

    public int updateTag(int id, int tag) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id",id);
        updateWrapper.set("tag_id",tag);
        return discussPostDao.update(null,updateWrapper);
    }

    public int getPostRows(String title,int type,int status,int tagId){
        QueryWrapper wrapper = new QueryWrapper();
        if (!"null".equals(title)){
            wrapper.like("title",title);
        }
        if(status!=-1){
            wrapper.eq("status",status);
        }
        if(type!=-1){
            wrapper.eq("type",type);
        }
        if(tagId!=-1){
            wrapper.eq("tag_id",tagId);
        }
        return discussPostDao.selectCount(wrapper);
    }

    public List<DiscussPost> getPostList(String title,int type,
                                         int status,int tagId,
                                         int current, int limit){
        QueryWrapper wrapper = new QueryWrapper();
        if (!"null".equals(title)){
            wrapper.like("title",title);
        }
        if(status!=-1){
            wrapper.eq("status",status);
        }
        if(type!=-1){
            wrapper.eq("type",type);
        }
        if(tagId!=-1){
            wrapper.eq("tag_id",tagId);
        }
        Page<DiscussPost> result = discussPostDao.selectPage(new Page<>(current,limit), wrapper);
        return result.getRecords();
    }

}
